﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace TRIG_MOD_CORRES.wsp
{
    class DataForCorresRegister
    {
        public RadioButtonList Sent_Receive { get; set; }

        public string frmDate { get; set; }
        public string toDate { get; set; }
        public string company { get; set; }
        public Label errorMsg { get; set; }

        public string subject { get; set; }

        public string cmlQry { get; set; }

        public DataTable dtResult = new DataTable();

        public void CreateTable()
        {
            dtResult = CreateDataColumns("Entity Name", "User", "Type", "Subject", "Document Details", "Created", "DocURL");
        }

        private static DataTable CreateDataColumns(params string[] colna)
        {
            DataTable dt = new DataTable();
            foreach (string item in colna)
            {
                dt.Columns.Add(item);
            }
            return dt;
        }

        public void getListItems(string lstName)
        {
            string entity = string.Empty; string user = string.Empty; string corresType = string.Empty; string subject = string.Empty; string created = string.Empty;

            string DocumentDetails = string.Empty;

            string SrcListName = string.Empty; string SrcItemID = string.Empty;

            SPFieldLookupValue users = null;

            using (SPSite oSite = new SPSite(SPContext.Current.Site.Url))
            {
                using (SPWeb oWeb = oSite.OpenWeb())
                {
                    try
                    {
                        SPListItemCollection olstItemCol = oWeb.Lists.TryGetList(lstName).GetItems(getCamlQry());
                        if (olstItemCol.Count > 0)
                        {
                            foreach (SPListItem olstItems in olstItemCol)
                            {
                                if (Sent_Receive.SelectedItem.Value == "0")
                                {
                                    entity = Convert.ToString(olstItems["From_x0020_Entity_x0020_Name"]);
                                    user = Convert.ToString(olstItems["From_x0020_User"]);
                                    users = new SPFieldLookupValue(user);
                                    user = users.LookupValue;
                                }
                                else
                                {
                                    entity = Convert.ToString(olstItems["To_x0020_Entity_x0020_Name"]);
                                    user = Convert.ToString(olstItems["To_x0020_User"]);
                                    users = new SPFieldLookupValue(user);
                                    user = users.LookupValue;
                                }

                                corresType = Convert.ToString(olstItems["Correspondence_x0020_Type"]);
                                subject = Convert.ToString(olstItems["Document_x0020_Subject"]);
                                created = Convert.ToString(olstItems["Created"]);

                                DocumentDetails = Convert.ToString(olstItems["Document_x0020_Details"]);
                                SPFieldUrlValue docURL = new SPFieldUrlValue(DocumentDetails);
                                string doc = docURL.Url;

                                dtResult.Rows.Add(entity, user, corresType, subject, docURL.Description, created, docURL.Url);
                            }
                        }
                        else
                        {
                            errorMsg.Text = "No records found"; errorMsg.ForeColor = System.Drawing.Color.Green;
                        }
                    }
                    catch (Exception x)
                    {
                        errorMsg.Text = "No records found" + x.Message; errorMsg.ForeColor = System.Drawing.Color.Red;
                    }

                }
            }
        }

        public void BindDate(GridView gv, string lstName)
        {
            CreateTable();
            getListItems(lstName);
            if (dtResult.Rows.Count > 0)
            {
                gv.Visible = true;
                gv.DataSource = dtResult;
                gv.DataBind();
                HideGridFields(gv);
            }
            else
            { errorMsg.Text = "No records found"; errorMsg.ForeColor = System.Drawing.Color.Green; }
        }

        private void HideGridFields(GridView gv)
        {
            gv.HeaderRow.Cells[gv.Rows[0].Cells.Count - 1].Visible = false;

            for (int i = 0; i < gv.Rows.Count; i++)
            {
                gv.Rows[i].Cells[gv.Rows[0].Cells.Count - 1].Visible = false;

            }
        }

        private SPQuery getCamlQry()
        {
            if (company == "CNT") { company = "contractor"; } else if (company == "CNS") { company = "consultant"; } else { company = "client"; }

            SPQuery qry = new SPQuery();

            if (string.IsNullOrEmpty(subject))
            {

                if (Sent_Receive.SelectedItem.Value == "0")
                    cmlQry = "<Where><And><Geq><FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">"+ frmDate +"</Value></Geq><And><Leq>"+
                        "<FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">"+ toDate +"</Value></Leq><And><Eq><FieldRef Name=\"From_x0020_Entity\" />"+
                        "<Value Type=\"Text\">"+ company +"</Value></Eq><Eq><FieldRef Name=\"To_x0020_User\" /><Value Type=\"User\">"+ SPContext.Current.Web.CurrentUser.LoginName +"</Value>"+
                        "</Eq></And></And></And></Where>";
                else
                    cmlQry = "<Where><And><Geq><FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">" + frmDate + "</Value></Geq><And><Leq>" +
                        "<FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">" + toDate + "</Value></Leq><And><Eq><FieldRef Name=\"To_x0020_Entity\" />" +
                        "<Value Type=\"Text\">" + company + "</Value></Eq><Eq><FieldRef Name=\"From_x0020_User\" /><Value Type=\"User\">" + SPContext.Current.Web.CurrentUser.LoginName + "</Value>" +
                        "</Eq></And></And></And></Where>";
            }

            else
            {
                if (Sent_Receive.SelectedItem.Value == "0")
                    cmlQry = "<Where><And><Geq><FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">"+ frmDate +"</Value></Geq><And><Leq>"+
                        "<FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">"+ toDate +"</Value></Leq><And><Eq><FieldRef Name=\"From_x0020_Entity\" />"+
                        "<Value Type=\"Text\">"+ company +"</Value></Eq><And><Eq><FieldRef Name=\"To_x0020_User\" /><Value Type=\"User\">"+ SPContext.Current.Web.CurrentUser.LoginName +"</Value>"+
                        "</Eq><Eq><FieldRef Name=\"Document_x0020_Subject\" /><Value Type=\"Text\">"+ subject +"</Value></Eq></And></And></And></And></Where>";
                else
                    cmlQry = "<Where><And><Geq><FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">" + frmDate + "</Value></Geq><And><Leq>" +
                        "<FieldRef Name=\"Created\" /><Value IncludeTimeValue=\"false\" Type=\"DateTime\">" + toDate + "</Value></Leq><And><Eq><FieldRef Name=\"To_x0020_Entity\" />" +
                        "<Value Type=\"Text\">" + company + "</Value></Eq><And><Eq><FieldRef Name=\"From_x0020_User\" /><Value Type=\"User\">" + SPContext.Current.Web.CurrentUser.LoginName + "</Value>" +
                        "</Eq><Eq><FieldRef Name=\"Document_x0020_Subject\" /><Value Type=\"Text\">" + subject + "</Value></Eq></And></And></And></And></Where>";

            }

            qry.Query = cmlQry;
            return qry;
        }

        public void CreateLinkButton(string LinkText, string OnClickScript, GridViewRowEventArgs e, int ColumnNumber, bool EnableButton)
        {
            LinkButton lb = new LinkButton();
            lb.Text = LinkText;
            if (OnClickScript.StartsWith("javascript:"))
                lb.OnClientClick = OnClickScript;
            else
                lb.Attributes.Add("onclick", "loadFile(" + OnClickScript + ");");
            lb.Enabled = EnableButton;
            //lb.Font.Underline = true;
            //lb.ForeColor = System.Drawing.Color.Blue;
            e.Row.Cells[ColumnNumber].Controls.Add((Control)lb);
        }

    }
}
